package com.wfh.api.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfh.api.common.ErrorCode;
import com.wfh.api.exception.BusinessException;
import com.wfh.api.mapper.InterfaceInfoMapper;
import com.wfh.api.model.dto.interfaceinfo.InterfaceInfoQueryRequest;
import com.wfh.api.model.entity.InterfaceInfo;
import com.wfh.api.model.entity.User;
import com.wfh.api.model.vo.InterfaceInfoVO;
import com.wfh.api.model.vo.UserVO;
import com.wfh.api.service.InterfaceInfoService;
import com.wfh.api.service.UserService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author lenovo
* @description 针对表【interface_info(接口信息)】的数据库操作Service实现
* @createDate 2025-05-30 15:26:55
*/
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
    implements InterfaceInfoService {



    @Resource
    private UserService userService;




    @Override
    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {
        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = interfaceInfo.getName();
        String description = interfaceInfo.getDescription();
        String url = interfaceInfo.getUrl();
        String requestHeader = interfaceInfo.getRequestHeader();
        // 校验各种参数
        if (add) {
           if (StringUtils.isAnyBlank(name)){
               throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口名称不能为空");
           }
            if (StringUtils.isAnyBlank(name) && name.length() < 50){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口名称不能为空且长度不能小于50");
            }
            if (StringUtils.isAnyBlank(description) && description.length() < 50){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口描述不能为空且长度不能小于50");
            }
            if (StringUtils.isAnyBlank(url) && url.length() < 50){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口地址不能为空且长度不能小于50");
            }
            if (StringUtils.isAnyBlank(requestHeader) && requestHeader.length() < 50){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口请求头不能为空且长度不能小于50");

            }
        }

    }


    /**
     * 获取查询包装类
     *
     * @param interfaceInfoQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<InterfaceInfo> getQueryWrapper(InterfaceInfoQueryRequest interfaceInfoQueryRequest) {
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        if (interfaceInfoQueryRequest == null) {
            return queryWrapper;
        }
        Long id = interfaceInfoQueryRequest.getId();
        String name = interfaceInfoQueryRequest.getName();
        String description = interfaceInfoQueryRequest.getDescription();
        String url = interfaceInfoQueryRequest.getUrl();
        Long userId = interfaceInfoQueryRequest.getUserId();
        // 拼接查询条件
        if (StringUtils.isNotBlank(description)) {
            queryWrapper.and(qw -> qw.like("description", description).or().like("description", description));
        }
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(url), "url", url);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId);
        return queryWrapper;
    }




    @Override
    public InterfaceInfoVO getInterfaceInfoVO(InterfaceInfo interfaceInfo, HttpServletRequest request) {
        long interfaceInfoId = interfaceInfo.getId();
        // 1. 关联查询用户信息
        Long userId = interfaceInfo.getUserId();
        User user = null;
        if (userId != null && userId > 0) {
            user = userService.getById(userId);
        }
        UserVO userVO = userService.getUserVO(user);
        InterfaceInfoVO interfaceInfoVO = this.getInterfaceInfoVO(interfaceInfo, request);
        return interfaceInfoVO;
    }

    @Override
    public Page<InterfaceInfoVO> getInterfaceInfoVOPage(Page<InterfaceInfo> interfaceInfoPage, HttpServletRequest request) {
        List<InterfaceInfo> interfaceInfoList = interfaceInfoPage.getRecords();
        Page<InterfaceInfoVO> interfaceInfoVOPage = new Page<>(interfaceInfoPage.getCurrent(), interfaceInfoPage.getSize(), interfaceInfoPage.getTotal());
        if (CollUtil.isEmpty(interfaceInfoList)) {
            return interfaceInfoVOPage;
        }
        return interfaceInfoVOPage;
    }



}




